<script setup lang="ts">
import { onHide, onLaunch, onShow } from "@dcloudio/uni-app";
import StudentApi from "@/api/student";
import { navigateToInterceptor } from "@/router/interceptor";
import { useConfigStore } from "@/store/config";
import { useStudentStore } from "@/store/student";
import { useTokenStore } from "@/store/token";
import { useUserStore } from "@/store/user";

const systemBarHeight = ref(40);
const tokenStore = useTokenStore();
const userStore = useUserStore();
const configStore = useConfigStore();
const studentStore = useStudentStore();

// 获取系统栏高度
function getSysteminfo() {
  // 获取存储的导航栏高度信息
  const storageInfo = uni.getStorageInfoSync();

  if (storageInfo && storageInfo?.navbarHeight) {
    // 如果存在高度信息，则赋值
    systemBarHeight.value = storageInfo.navbarHeight;
  } else {
    uni.getSystemInfo({
      success: (res) => {
        systemBarHeight.value = res.statusBarHeight;
        uni.setStorageSync("navbarHeight", res.statusBarHeight);
      },
    });
  }
}
// onMounted(async () => {

// })
onLaunch(async (options) => {
  console.log("App Launch", options);

  getSysteminfo();
  // tokenStore.wxLogin();
  // 这段代码在组件挂载后执行（类似于mounted）
  console.log("configStore.loginRole", configStore.loginRole, configStore);
  if (!tokenStore.validToken || !userStore.userInfo.unionId) {
    tokenStore.wxLogin();
  }
  if (userStore.userInfo.userId) {
    // 直接跳到教师页面
    configStore.switchRole("2");
    uni.reLaunch({ url: "/pages/index/index" });
    // options.url = '/pages/teacherHome/index'
  } else if (configStore.loginRole !== "2") {
    // 家长登录
    const res = await StudentApi.getPersonnels();
    console.log("res", res);
    if (res.length > 0) {
      studentStore.addPersonnelIds(res);
      uni.switchTab({ url: "/pages/index/index" });
      // uni.reLaunch({ url: '/pages-fg/login/role' })
      console.log("跳转到首页");
    } else {
      uni.reLaunch({ url: "/pages-fg/login/role" });
    }
  } else {
    // 教师登录
    uni.reLaunch({ url: "/pages-fg/login/role" });
  }
});

onShow((options) => {
  console.log("App Show", options);
  // 处理直接进入页面路由的情况：如h5直接输入路由、微信小程序分享后进入等
  // https://github.com/unibest-tech/unibest/issues/192
  // if (options?.path) {
  //   navigateToInterceptor.invoke({
  //     url: `/${options.path}`,
  //     query: options.query,
  //   })
  // }
  // else
  // navigateToInterceptor.invoke({ url: '/' })
});
onHide(() => {
  console.log("App Hide");
});
</script>

<style lang="scss">
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}
</style>

function created() { throw new Error('Function not implemented.') }
